<?php
/**
*管理员类
*/
namespace app\admin\controller;
use think\Validate;

class Admin extends Common
{
    public function main()
    {
        if (request()->isAjax()){
            $limit=input('limit');
            $limit=$limit?$limit:10;
            $page=input('page');
            $data=db('admin')->limit($limit)->page($page)->select();
            $count=model('admin')->count();
            //处理需特别处理的数据
            foreach ($data as $k=>$v) {
                $data[$k]['create_time']=$this->timeFormat($v['create_time']);
                $data[$k]['last_login']=$this->timeFormat($v['last_login']);
            }
            $res["msg"] = "";
            $res["code"] = 0;
            $res["count"] = $count;
            $res["data"] = $data;
            if(empty($data)){
                $res["msg"]="暂无数据";
            }
            return json($res);
        }
      return view('main');
    }
    public function add(){
        if(request()->isAjax()){
            $data=input('post.');
            $data['password']=md5($data['password']);
            $data['create_time']=time();
            $data['last_login']=time();
            //定义错误验证规则
            $rule=[
                ['username','require|max:8|unique:admin','用户名必须填写!|最大长度不得超过50个字符|当前用户名已经存在'],
                ['nick_name','require|max:50|unique:admin','昵称必须填写!|最大长度不得超过50个字符|很遗憾当前昵称已经被使用!'],
                ['password','min:6','密码长度最小为6位'],
                ['group_id','require','用户组必须选择'],
            ];
            $validate=new Validate($rule);
            $validate_result=$validate->check($data);
            if (!$validate_result) {
                //如果验证失败就获取错误提示消息
                $res=['code'=>1,'msg'=>$this->error($validate->getError())];
                return json($res);
            }
            $add=db('admin')->insert($data);
            if($add){
                $res=[
                    'code'=>200,
                    'msg' => '添加管理员成功'
                ];
                return $res;
            }else{
                return json(array('code' => 0, 'msg' => '添加管理员失败'));
            }
        }
//        $group=db('group')->field('id,group_name')->select();
//        $this->assign(['group'=>$group]);
        return view();
    }
    public function  edit(){
        $id=input('id');
        if(request()->isAjax()){
            $data=input('post.');
            //定义错误验证规则
            $rule=[
                ['username','require|max:8|unique:admin','用户名必须填写!|最大长度不得超过50个字符|当前用户名已经存在'],
                ['nick_name','require|max:50|unique:admin','昵称必须填写!|最大长度不得超过50个字符|很遗憾当前昵称已经被使用!'],
                //['password','min:6','密码长度最小为6位'],
                ['group_id','require','用户组必须选择'],
            ];
            $validate=new Validate($rule);
            $validate_result=$validate->check($data);
            if (!$validate_result) {
                //如果验证失败就获取错误提示消息
                $res=['code'=>1,'msg'=>$this->error($validate->getError())];
                return json($res);
            }
            if($data['password']){
                $data['password']=md5($data['password']);
            }else{
                unset($data['password']);
            }
            $update=db('admin')->where('id',$data['id'])->update($data);
            if ($update){
                $res=['code'=>200,'msg'=>"更新数据成功!"];
            }else{
                $res=['code'=>1,'msg'=>"更新数据失败!"];
            }
            return json($res);
        }
        $data=db('admin')->find($id);
        $this->assign('data', $data);
        return view('edit');
    }
    public function del(){
        $id=input('id');
        if($id==1){
            $res=['status'=>1,'msg'=>"超级管理员账户，不能删除!"];
        }else{
            $del=db('admin')->delete($id);
            if($del){
                $res=['status'=>200,'msg'=>"删除数据成功!"];
            }else{
                $res=['status'=>1,'msg'=>"删除数据失败!"];
            }
        }
        return json($res);
    }
    //通过checkbox修改状态
    public function statusChange(){
        $id=input('id');
        $admins=db('admin')->field('status')->find($id);
        $status=$admins['status'];
        //禁用为2，启用为1
        if($status==1){
            $up=db('admin')->where('id',$id)->update(['status'=>2]);
            if ($up){
                return $res=['code'=>200,'msg'=>'状态修改成功!'];
            }else{
                return $res=['code'=>1,'msg'=>'状态修改失败!'];
            }

        }else{
            $up=db('admin')->where('id',$id)->update(['status'=>1]);
            if ($up){
                return $res=['code'=>200,'msg'=>'状态修改成功!'];
            }else{
                return $res=['code'=>1,'msg'=>'状态修改失败!'];
            }
        }
        return json($res);
    }

    public function  test(){
        $data=['username'=>'1234','password'=>'1234'];
        $rule=[
            ['username','require|max:50|unique:admin','用户名必须填写!|最大长度不得超过50个字符|用户名必须是唯一的'],
            ['password','min:6','密码长度最小为6位'],
        ];
        $validate=new Validate($rule);
        $vas=$validate->check($data);
        if (!$vas) {
//            return json(array(
//                "info" => $this->error($validate->getError())
//            ));//这里要用json处理下
            return $this->error($validate->getError());//这里要用json处理下
        }else{
            echo "验证成功!";

        }
    }
}
